<template>
  <div>
    <!-- title是对话框的标题 -->
    <!-- visible: 是否显示, sync同步控制 -->
    <el-dialog
      :title="msg.type == 'add' ? '添加管理员':'修改管理员' "
      :visible.sync="msg.isShow"
      width="50%"
    >
    <!-- model: 表单的变量 -->
    <!-- rules: 校验的规则 -->
    <el-form 
    ref="ruleFrom" 
    :model="ruleFrom" 
    :rules="rules"
    label-width="100px">
  
    <el-form-item  label="角色名称" >
      <!-- 要添加的 -->
        <el-select v-model="ruleFrom.roleid" prop='roleid'  placeholder="请选角色名称">
          <el-option v-for="(role,index) in msg.roleList" :key="index" :label="role.rolename" :value="role.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="管理员名称" prop="username">
        <el-input type="text" v-model="ruleFrom.username"></el-input>
      </el-form-item>
      <el-form-item label="管理员密码" prop="password">
        <el-input type="password" v-model="ruleFrom.password"></el-input>
      </el-form-item>


      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="ruleFrom.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item>
        <el-button type="success" @click="onSubmit">立即创建</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item> -->
    </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button type="success" v-if="msg.type == 'add'" @click="submit">立即添加</el-button>
          <el-button type="success" v-if="msg.type == 'edit'" @click="submit">立即修改</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import {editAdminListApi} from '../../api'
export default {
  props:['msg'],
     data() {
       return {
         resetVal:{},
         // 表单数据
        ruleFrom: {
          roleid:3,
          username: "",
          password: "",
          status: 1,
        },
         rules: {
          username: [ // required 必填项, message 是报错信息,  trigger 是触发方式
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          roleid: [ // required 必填项, message 是报错信息,  trigger 是触发方式
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
        },

      }
    },

    methods: {
      setVal(info){
        this.resetVal = info;
        // 此处info是 list 传回的表单数据 tableData  引用数据类型 直接赋值是 数据地址赋值  ruleFrom 绑定v-model  修改 tableData也会事实修改  使用...解构赋值可以解决此问题
        this.ruleFrom = {...info};
      },
      submit(){

        this.$refs.ruleFrom.validate(async (val) => {
          if(val){

            if(this.msg.type == 'add'){

              this.http.post('/useradd',this.ruleFrom)
                .then(res => {
                  if(res.code == 200){
                     this.$message({
                       type: "success",
                       message: res.msg,
                     });
                  }else{
                     this.$message({
                       type: "warning",
                       message: res.msg,
                     });
                  }
                  this.msg.isShow = false;
                })
                .catch(err => {
                  console.log(err);
                  this.$message({
                       type: "warning",
                       message: err.msg,
                     });
                })
            }else{
              let res = await editAdminListApi(this.ruleFrom);
               this.$message({
                type: "success",
                message: res.msg,
              });
              this.msg.isShow = false;
            }
            this.$emit('update');

          }else{
            // 校验失败
            return false;
          }

        })


      },
      reset(){
        this.ruleFrom = {...this.resetVal}
      }

    },
    



};
</script>

<style>
</style>